home *** CD-ROM | disk | FTP | other *** search
/ Aminet 3 / Aminet 3 - July 1994.iso / Aminet / util / cli / ATT_awk_1_0.lha / AT&T-awk / awk.man < prev    next >
Encoding:
Text File  |  1994-06-02  |  13.0 KB  |  331 lines

  1.  
  2.  
  3.  
  4. AWK(1)                                                     AWK(1)
  5.  
  6.  
  7. NNAAMMEE
  8.        awk - pattern-directed scanning and processing language
  9.  
  10. SSYYNNOOPPSSIISS
  11.        aawwkk  [ --FF _f_s ] [ --vv _v_a_r_=_v_a_l_u_e ] [ _'_p_r_o_g_' | --ff _p_r_o_g_f_i_l_e ] [
  12.        _f_i_l_e _._._.  ]
  13.  
  14. DDEESSCCRRIIPPTTIIOONN
  15.        _A_w_k scans each input _f_i_l_e for lines that match  any  of  a
  16.        set  of  patterns specified literally in _p_r_o_g or in one or
  17.        more files specified as --ff _p_r_o_g_f_i_l_e.   With  each  pattern
  18.        there  can  be an associated action that will be performed
  19.        when a line of a _f_i_l_e matches the pattern.  Each  line  is
  20.        matched  against  the  pattern  portion  of every pattern-
  21.        action statement; the associated action is  performed  for
  22.        each  matched  pattern.   The file name means the standard
  23.        input.  Any _f_i_l_e of the form _v_a_r_=_v_a_l_u_e is  treated  as  an
  24.        assignment, not a filename, and is executed at the time it
  25.        would have been opened if it were a filename.  The  option
  26.        --vv  followed  by  _v_a_r_=_v_a_l_u_e  is  an  assignment to be done
  27.        before _p_r_o_g is executed; any number of --vv options  may  be
  28.        present.  The --FF _f_s option defines the input field separa­
  29.        tor to be the regular expression _f_s_.
  30.  
  31.        An input line is normally made up of fields  separated  by
  32.        white space.  (This default can be changed by using the FS
  33.        built-in variable or the --FF _f_s option.)   The  fields  are
  34.        denoted $$11, $$22, ..., while $$00 refers to the entire line.
  35.  
  36.        A pattern-action statement has the form
  37.  
  38.               _p_a_t_t_e_r_n {{ _a_c_t_i_o_n }}
  39.  
  40.        A  missing {{ _a_c_t_i_o_n }} means print the line; a missing pat­
  41.        tern always matches.  Pattern-action statements are  sepa­
  42.        rated by newlines or semicolons.
  43.  
  44.        An action is a sequence of statements.  A statement can be
  45.        one of the following:
  46.  
  47.               if( _e_x_p_r_e_s_s_i_o_n ) _s_t_a_t_e_m_e_n_t [ else _s_t_a_t_e_m_e_n_t ]
  48.               while( _e_x_p_r_e_s_s_i_o_n ) _s_t_a_t_e_m_e_n_t
  49.               for( _e_x_p_r_e_s_s_i_o_n ; _e_x_p_r_e_s_s_i_o_n ; _e_x_p_r_e_s_s_i_o_n ) _s_t_a_t_e_m_e_n_t
  50.               for( _v_a_r in _a_r_r_a_y ) _s_t_a_t_e_m_e_n_t
  51.               do _s_t_a_t_e_m_e_n_t while( _e_x_p_r_e_s_s_i_o_n )
  52.               break
  53.               continue
  54.               { [ _s_t_a_t_e_m_e_n_t _._._. ] }
  55.               _e_x_p_r_e_s_s_i_o_n              # commonly _v_a_r _= _e_x_p_r_e_s_s_i_o_n
  56.               print [ _e_x_p_r_e_s_s_i_o_n_-_l_i_s_t ] [ > _e_x_p_r_e_s_s_i_o_n ]
  57.               printf _f_o_r_m_a_t [ , _e_x_p_r_e_s_s_i_o_n_-_l_i_s_t ] [ > _e_x_p_r_e_s_s_i_o_n ]
  58.               return [ _e_x_p_r_e_s_s_i_o_n ]
  59.               next                    # skip remaining patterns on this input line
  60.               delete _a_r_r_a_y[ _e_x_p_r_e_s_s_i_o_n ]# delete an array element
  61.  
  62.  
  63.  
  64.                                                                 1
  65.  
  66.  
  67.  
  68.  
  69.  
  70. AWK(1)                                                     AWK(1)
  71.  
  72.  
  73.               exit [ _e_x_p_r_e_s_s_i_o_n ]     # exit immediately; status is _e_x_p_r_e_s_s_i_o_n
  74.  
  75.        Statements are terminated by semicolons, newlines or right
  76.        braces.   An  empty _e_x_p_r_e_s_s_i_o_n_-_l_i_s_t stands for $$00.  String
  77.        constants are quoted "" "", with the usual C escapes  recog­
  78.        nized  within.  Expressions take on string or numeric val­
  79.        ues as appropriate, and are built using the operators ++  --
  80.        **  // %% ^^ (exponentiation), and concatenation (indicated by
  81.        a blank).  The operators !! ++++ ---- ++== --== **== //== %%== ^^== >> >>==  <<
  82.        <<==  ==== !!== ??:: are also available in expressions.  Variables
  83.        may be scalars, array elements (denoted _x[[_i]]_)  or  fields.
  84.        Variables  are initialized to the null string.  Array sub­
  85.        scripts may be any string, not necessarily  numeric;  this
  86.        allows  for  a  form of associative memory.  Multiple sub­
  87.        scripts such as [[ii,,jj,,kk]] are  permitted;  the  constituents
  88.        are concatenated, separated by the value of SSUUBBSSEEPP.
  89.  
  90.        The  pprriinntt  statement prints its arguments on the standard
  91.        output (or on a file if >>_f_i_l_e or >>>>_f_i_l_e is present or on a
  92.        pipe  if ||_c_m_d is present), separated by the current output
  93.        field separator, and terminated by the output record sepa­
  94.        rator.  _f_i_l_e and _c_m_d may be literal names or parenthesized
  95.        expressions; identical string values in  different  state­
  96.        ments  denote  the  same  open file.  The pprriinnttff statement
  97.        formats its expression list according to the  format  (see
  98.        _p_r_i_n_t_f(3))_.   The built-in function cclloossee((_e_x_p_r)) closes the
  99.        file or pipe _e_x_p_r.
  100.  
  101.        The mathematical functions eexxpp, lloogg, ssqqrrtt, ssiinn,  ccooss,  and
  102.        aattaann22 are built in.  Other built-in functions:
  103.  
  104.        lleennggtthh the length of its argument taken as a string, or of
  105.               $$00 if no argument.
  106.  
  107.        rraanndd   random number on (0,1)
  108.  
  109.        ssrraanndd  sets seed for rraanndd and returns the previous seed.
  110.  
  111.        iinntt    truncates to an integer value
  112.  
  113.        ssuubbssttrr((_s,, _m,, _n))
  114.               the _n-character substring of _s that begins at posi­
  115.               tion _m counted from 1.
  116.  
  117.        iinnddeexx((_s,, _t))
  118.               the  position  in _s where the string _t occurs, or 0
  119.               if it does not.
  120.  
  121.        mmaattcchh((_s,, _r))
  122.               the position in _s where the  regular  expression  _r
  123.               occurs,  or 0 if it does not.  The variables RRSSTTAARRTT
  124.               and RRLLEENNGGTTHH are set to the position and  length  of
  125.               the matched string.
  126.  
  127.  
  128.  
  129.  
  130.                                                                 2
  131.  
  132.  
  133.  
  134.  
  135.  
  136. AWK(1)                                                     AWK(1)
  137.  
  138.  
  139.        sspplliitt((_s,, _a,, _f_s))
  140.               splits the string _s into array elements _a[[11]]_, _a[[22]]_,
  141.               ..., _a[[_n]]_, and returns _n.  The separation  is  done
  142.               with  the  regular  expression _f_s or with the field
  143.               separator FFSS if _f_s is not given.
  144.  
  145.        ssuubb((_r,, _t,, _s))
  146.               substitutes _t for the first occurrence of the regu­
  147.               lar  expression  _r  in  the  string _s.  If _s is not
  148.               given, $$00 is used.
  149.  
  150.        ggssuubb   same as ssuubb except that all occurrences of the reg­
  151.               ular  expression  are replaced; ssuubb and ggssuubb return
  152.               the number of replacements.
  153.  
  154.        sspprriinnttff((_f_m_t,, _e_x_p_r,, _._._. ))
  155.               the  string  resulting  from  formatting  _e_x_p_r  _._._.
  156.               according to the _p_r_i_n_t_f(3) format _f_m_t
  157.  
  158.        ssyysstteemm((_c_m_d))
  159.               executes _c_m_d and returns its exit status
  160.  
  161.        The  ``function'' ggeettlliinnee sets $$00 ttoo the next input record
  162.        from the current input file; ggeettlliinnee <<_f_i_l_e sets $$00 to  the
  163.        next record from _f_i_l_e.  ggeettlliinnee _x sets variable _x instead.
  164.        Finally, _c_m_d || ggeettlliinnee pipes the output of _c_m_d  into  ggeett­­
  165.        lliinnee; each call of ggeettlliinnee returns the next line of output
  166.        from _c_m_d.  In all cases, ggeettlliinnee returns 1 for a  success­
  167.        ful input, 0 for end of file, and -1 for an error.
  168.  
  169.        Patterns are arbitrary Boolean combinations (with !! |||| &&&&)
  170.        of regular expressions and relational expressions.   Regu­
  171.        lar  expressions  are  as in _e_g_r_e_p; see _g_r_e_p(1).  Isolated
  172.        regular expressions in a pattern apply to the entire line.
  173.        Regular  expressions  may also occur in relational expres­
  174.        sions, using the operators ~~ and !!~~.  //_r_e// is  a  constant
  175.        regular  expression; any string (constant or variable) may
  176.        be used as a regular expression, except in the position of
  177.        an isolated regular expression in a pattern.
  178.  
  179.        A  pattern  may  consist  of  two  patterns separated by a
  180.        comma; in this case, the action is performed for all lines
  181.        from  an  occurrence of the first pattern though an occur­
  182.        rence of the second.
  183.  
  184.        A relational expression is one of the following:
  185.  
  186.               _e_x_p_r_e_s_s_i_o_n _m_a_t_c_h_o_p _r_e_g_u_l_a_r_-_e_x_p_r_e_s_s_i_o_n
  187.               _e_x_p_r_e_s_s_i_o_n _r_e_l_o_p _e_x_p_r_e_s_s_i_o_n
  188.               _e_x_p_r_e_s_s_i_o_n iinn _a_r_r_a_y_-_n_a_m_e
  189.               ((_e_x_p_r,,_e_x_p_r_,_._._.)) iinn _a_r_r_a_y_-_n_a_m_e
  190.  
  191.        where a relop is any of the six relational operators in C,
  192.        and  a  matchop  is  either  ~~  (matches)  or !!~~ (does not
  193.  
  194.  
  195.  
  196.                                                                 3
  197.  
  198.  
  199.  
  200.  
  201.  
  202. AWK(1)                                                     AWK(1)
  203.  
  204.  
  205.        match).  A conditional  is  an  arithmetic  expression,  a
  206.        relational  expression, or a Boolean combination of these.
  207.  
  208.        The special patterns BBEEGGIINN and EENNDD may be used to  capture
  209.        control  before the first input line is read and after the
  210.        last.  BBEEGGIINN and EENNDD do not combine with other patterns.
  211.  
  212.        Variable names with special meanings:
  213.  
  214.        FFSS     regular expression used to  separate  fields;  also
  215.               settable by option --FF_f_s_.
  216.  
  217.        NNFF     number of fields in the current record
  218.  
  219.        NNRR     ordinal number of the current record
  220.  
  221.        FFNNRR    ordinal number of the current record in the current
  222.               file
  223.  
  224.        FFIILLEENNAAMMEE
  225.               the name of the current input file
  226.  
  227.        RRSS     input record separator (default newline)
  228.  
  229.        OOFFSS    output field separator (default blank)
  230.  
  231.        OORRSS    output record separator (default newline)
  232.  
  233.        OOFFMMTT   output format for numbers (default %%..66gg)
  234.  
  235.        SSUUBBSSEEPP separates multiple subscripts (default 034)
  236.  
  237.        AARRGGCC   argument count, assignable
  238.  
  239.        AARRGGVV   argument array, assignable;  non-null  members  are
  240.               taken as filenames
  241.  
  242.        EENNVVIIRROONN
  243.               array  of  environment  variables;  subscripts  are
  244.               names.
  245.  
  246.        Functions may be defined (at the position  of  a  pattern-
  247.        action statement) thus:
  248.  
  249.               function foo(a, b, c) { ...; return x }
  250.  
  251.        Parameters  are passed by value if scalar and by reference
  252.        if  array  name;  functions  may  be  called  recursively.
  253.        Parameters  are local to the function; all other variables
  254.        are global.  Thus local variables may be created  by  pro­
  255.        viding excess parameters in the function definition.
  256.  
  257. EEXXAAMMPPLLEESS
  258.  
  259.  
  260.  
  261.  
  262.                                                                 4
  263.  
  264.  
  265.  
  266.  
  267.  
  268. AWK(1)                                                     AWK(1)
  269.  
  270.  
  271.        length > 72
  272.               Print lines longer than 72 characters.
  273.  
  274.        { print $2, $1 }
  275.               Print first two fields in opposite order.
  276.  
  277.        BEGIN { FS = ",[ \t]*|[ \t]+" }
  278.              { print $2, $1 }
  279.  
  280.               Same,  with  input fields separated by comma and/or
  281.               blanks and tabs.
  282.  
  283.             { s += $1 }
  284.        END  { print "sum is", s, " average is", s/NR }
  285.               Add up first column, print sum and average.
  286.  
  287.        /start/, /stop/
  288.               Print all lines between start/stop pairs.
  289.  
  290.        BEGIN     {    # Simulate echo(1)
  291.             for (i = 1; i < ARGC; i++) printf "%s ", ARGV[i]
  292.             printf "\n"
  293.             exit }
  294.  
  295. SSEEEE AALLSSOO
  296.        _l_e_x(1), _s_e_d(1)
  297.        A. V. Aho, B. W. Kernighan, P. J. Weinberger, _T_h_e _A_W_K _P_r_o_­
  298.        _g_r_a_m_m_i_n_g _L_a_n_g_u_a_g_e_, Addison-Wesley, 1988.
  299.  
  300. BBUUGGSS
  301.        There  are  no  explicit  conversions  between numbers and
  302.        strings.  To force an expression to be treated as a number
  303.        add  0  to  it; to force it to be treated as a string con­
  304.        catenate """" to it.
  305.        The scope rules for variables in functions  are  a  botch;
  306.        the syntax is worse.
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.                                                                 5
  329.  
  330.  
  331.